﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MM.Common.Base;
using DevExpress.LookAndFeel;
using Microsoft.Win32;

namespace MMTJ
{
    public partial class FrmXLogin : XtraForm
    {
        private int loginNum;
        protected string dataSource;
        public FrmXLogin()
        {
            InitializeComponent();
            this.loginNum = 0;
            if (DateTime.Now.AddMonths(3) > SystemInfo.LastDate)
            {
                Text = "MXHLY --- [" + SystemInfo.SystemCode + "]" + SystemInfo.SystemName + "登录[到期日期:" + SystemInfo.LastDate.ToString("yyyy年MM月dd日") + "]";
            }
            else
            {
                Text = "MXHLY --- [" + SystemInfo.SystemCode + "]" + SystemInfo.SystemName + "登录";
            }
        }

        public FrmXLogin(string datasource)
        {
            InitializeComponent();
            loginNum = 0;
            this.dataSource = datasource;
            if (DateTime.Now.AddMonths(3) > SystemInfo.LastDate)
            {
                Text = "MXHLY --- [" + SystemInfo.SystemCode + "]" + SystemInfo.SystemName + "登录[到期日期:" + SystemInfo.LastDate.ToString("yyyy年MM月dd日") + "]";
            }
            else
            {
                Text = "MXHLY --- [" + SystemInfo.SystemCode + "]" + SystemInfo.SystemName + "登录";
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnLog_Click(object sender, EventArgs e)
        {
            this.submit();
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.txtPassword.Focus();
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.submit();
            }
        }

        /// <summary>
        /// 登录事件
        /// </summary>
        private void submit()
        {
            bool flag = false;
            string text = txtID.Text;
            try
            {
                if (text.Length > 0)
                {
                    string str2 = "";
                    if (BaseDataHelper.OperLogin(txtID.Text, txtPassword.Text, ref str2))
                    {
                        SystemInfo.OrgHeadCode = ConfigFunction.GetSystemConfig("OrgHeadCode");
                        SystemInfo.OrgSubCode = ConfigFunction.GetComputerConfig("OrgSubCode", SystemInfo.OrgHeadCode, "机器对应分院代码");
                        if (SystemInfo.OrgSubCode == string.Empty)
                        {
                            SystemInfo.OrgSubCode = SystemInfo.OrgHeadCode;
                        }
                        object obj2 = SystemInfo.DbData.GetValue("KTSYS_ORGANIZATION", "ORG_CODE", SystemInfo.OrgSubCode, "ORG_NAME");
                        if (obj2 is string)
                        {
                            SystemInfo.OrgSubName = obj2.ToString();
                        }
                        SystemInfo.ProvinceCode = ConfigFunction.GetSystemConfig("ProvinceCode");
                        SystemInfo.CityCode = ConfigFunction.GetSystemConfig("CityCode");
                        SystemInfo.CountyCode = ConfigFunction.GetSystemConfig("CountyCode");
                        SystemInfo.TownCode = ConfigFunction.GetSystemConfig("TownCode");
                        flag = true;
                        this.start_with_windows();
                    }
                    else
                    {
                        loginNum++;
                        XtraMessageBox.Show(str2, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        txtPassword.Text = "";
                        txtID.Text = "";
                        txtID.Focus();
                    }
                }
            }
            catch (Exception exception)
            {
                XtraMessageBox.Show(exception.Message, "出错");
            }
            if (flag)
            {
                SystemInfo.OperRoleList = BaseDataHelper.GetOperRoleList();
                if ((ConfigFunction.GetSystemConfig("Bind_Computer", "N", "机器使用权", "C") == "Y") && BaseDataHelper.CheckComputerUsed(SystemInfo.ComputerID, SystemInfo.OperRoleList))
                {
                    XtraMessageBox.Show("对不起! 你没有在本机使用系统的权力!", "提示");
                    flag = false;
                }
                if (flag)
                {
                    Log.WriteLogSysInfo("登录" + SystemInfo.SystemName);
                    base.DialogResult = DialogResult.OK;
                    base.Close();
                }
            }
            else if (loginNum >= 3)
            {
                base.Close();
            }
        }

        /// <summary>
        /// 开机自动运行
        /// </summary>
        public void start_with_windows()
        {
            try
            {
                if (this.chkisauto.Checked)
                {
                    RegistryKey localMachine = Registry.LocalMachine;
                    RegistryKey key2 = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    if (key2.GetValue("MM软件") == null)
                    {
                        string executablePath = Application.ExecutablePath;
                        key2.SetValue("MM软件", executablePath);
                    }
                    else
                    {
                        key2.DeleteValue("MM软件");
                        string str2 = Application.ExecutablePath;
                        key2.SetValue("MM软件", str2);
                    }
                    key2.Close();
                    localMachine.Close();
                }
                else
                {
                    RegistryKey key3 = Registry.LocalMachine;
                    RegistryKey key4 = key3.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    if (key4.GetValue("MM软件") != null)
                    {
                        key4.DeleteValue("MM软件");
                    }
                    key4.Close();
                    key3.Close();
                }
            }
            catch
            {
            }
        }

    }
}
